/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"13701133-44E4-4DB3-9A63-FD6E0A682CEF",variableType:4}
 */
var fSoloMes = 1;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"270B32E3-D757-4C6E-A715-98F82E10002C",variableType:4}
 */
var fForma = 1;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"9B5B2293-67C7-4051-B0A3-A63B4C8E01C1",variableType:4}
 */
var fAsientoCierre = 0;

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"B2FC1D28-D61F-45EC-B765-77BC9E196155"}
 */
function buscar(event) {
	var $titulo
	var $sql = 'SELECT  s.cuentas_id, cp.codigo || \' \' || cp.nombre as "cap", scp.codigo || \' \' || scp.descripcion as "sub", '+
				'r.codigo || \' \' || r.descripcion as "rub", c.cuenta_impresion, SUM(s.debe_pes) as "debe_pes", SUM(s.debe_dol) as "debe_dol", '+
				'SUM(s.haber_pes) as "haber_pes", SUM(s.haber_dol) as "haber_dol", SUM(s.saldo) as "saldo", '+
				'c.capitulo, c.subcapitulo, c.rubro '+
				'FROM saldos "s" '+
				'INNER JOIN cuentas "c" ON s.cuentas_id = c.cuenta_id '+
				'INNER JOIN capitulo "cp" ON cp.codigo = c.capitulo '+
				'INNER JOIN subcapitulo "scp" ON scp.codigo = c.subcapitulo AND scp.capitulo = c.capitulo '+
				'INNER JOIN rubro "r" ON r.codigo = c.rubro AND r.subcapitulo = c.subcapitulo AND r.capitulo = c.capitulo '+
				'WHERE s.empresa = ? '
	
	var $where
	var $args_w
	if (fSoloMes == 1)
	{
		$where 	= 'AND ejercicio_id = ? AND anio = ? AND mes <= ? '
		$titulo = ' del ' + application.getValueListDisplayValue('mes_anio',globals.gMes+'|'+globals.gAnio) + ' de '+ globals.gAnio
		$args_w = [globals.gEjercicio, globals.gAnio, globals.gMes]
	}
	else
	{
		$where 	= 'AND ejercicio_id = ? AND (anio < ? OR anio = ? AND mes <= ?) '
		$titulo = ' hasta ' + application.getValueListDisplayValue('mes_anio',globals.gMes+'|'+globals.gAnio) + ' de '+ globals.gAnio
		$args_w = [globals.gEjercicio, globals.gAnio, globals.gAnio, globals.gMes]
	}
	$sql += $where
	$sql += 'GROUP BY s.cuentas_id, cap, sub, rub, c.cuenta_impresion, c.capitulo, c.subcapitulo, c.rubro '
	
	var $args = [globals.gUservID]
	$args = $args.concat($args_w)
	
	$sql += 'UNION ALL '
		
	$sql += 'SELECT  c.cuenta_id, cp.codigo || \' \' || cp.nombre as "cap", scp.codigo || \' \' || scp.descripcion as "sub", '+
			'r.codigo || \' \' || r.descripcion as "rub",	c.cuenta_impresion, '+
			'(SELECT COALESCE(SUM(s2.debe_pes),0) FROM saldos "s2" INNER JOIN cuentas "c2" ON s2.cuentas_id = c2.cuenta_id WHERE c2.capitulo = c.capitulo AND c2.subcapitulo = c.subcapitulo AND c2.rubro = c.rubro AND c2.cuenta = c.cuenta ' + $where + ' GROUP BY c2.cuenta) as "debe_pes", '+
			'(SELECT COALESCE(SUM(s2.debe_dol),0) FROM saldos "s2" INNER JOIN cuentas "c2" ON s2.cuentas_id = c2.cuenta_id WHERE c2.capitulo = c.capitulo AND c2.subcapitulo = c.subcapitulo AND c2.rubro = c.rubro AND c2.cuenta = c.cuenta ' + $where + 'GROUP BY c2.cuenta) as "debe_dol", '+
			'(SELECT COALESCE(SUM(s2.haber_pes),0) FROM saldos "s2" INNER JOIN cuentas "c2" ON s2.cuentas_id = c2.cuenta_id WHERE c2.capitulo = c.capitulo AND c2.subcapitulo = c.subcapitulo AND c2.rubro = c.rubro AND c2.cuenta = c.cuenta ' + $where + 'GROUP BY c2.cuenta) as "haber_pes", '+
			'(SELECT COALESCE(SUM(s2.haber_dol),0) FROM saldos "s2" INNER JOIN cuentas "c2" ON s2.cuentas_id = c2.cuenta_id WHERE c2.capitulo = c.capitulo AND c2.subcapitulo = c.subcapitulo AND c2.rubro = c.rubro AND c2.cuenta = c.cuenta ' + $where + 'GROUP BY c2.cuenta) as "haber_dol", '+
			'(SELECT COALESCE(SUM(s2.saldo),0) FROM saldos "s2" INNER JOIN cuentas "c2" ON s2.cuentas_id = c2.cuenta_id WHERE c2.capitulo = c.capitulo AND c2.subcapitulo = c.subcapitulo AND c2.rubro = c.rubro AND c2.cuenta = c.cuenta ' + $where + ' GROUP BY c2.cuenta) as "saldo", '+
			'c.capitulo, c.subcapitulo, c.rubro '+
			'FROM cuentas "c" '+
			'LEFT JOIN saldos "s" ON s.cuentas_id = c.cuenta_id '+
			'INNER JOIN capitulo "cp" ON cp.codigo = c.capitulo '+
			'INNER JOIN subcapitulo "scp" ON scp.codigo = c.subcapitulo AND scp.capitulo = c.capitulo '+
			'INNER JOIN rubro "r" ON r.codigo = c.rubro AND r.codigo = c.subcapitulo AND r.capitulo = c.capitulo '+
			'WHERE c.empresa = ? AND marca_sub = 1 '+
			'GROUP BY c.cuenta_id, cap, sub, rub, c.cuenta_impresion, c.capitulo, c.subcapitulo, c.rubro, c.cuenta '

	$sql += 'ORDER BY cuenta_impresion'
	
	$args = $args.concat($args_w)
	$args = $args.concat($args_w)
	$args = $args.concat($args_w)
	$args = $args.concat($args_w)
	$args = $args.concat($args_w)
	
	$args.push(globals.gUservID)
	var $ds = databaseManager.getDataSetByQuery('contable',$sql,$args,-1)
	
	if (globals.gmoneda_consulta == 1)
	{
		$ds.setColumnName(6,'importe_debe')
		$ds.setColumnName(8,'importe_haber')
	}
	else
	{
		$ds.setColumnName(7,'importe_debe')
		$ds.setColumnName(9,'importe_haber')
	}
	
	var $params = {
		userv: usuarios_to_uservs.razon_social,
		titulo: 'Balance'+ $titulo
	}
	globals.metodoJasper('impresion','contable/balance.jasper','Balance',$ds,$params)
	
}

/**
 * Handle changed data.
 *
 * @param {String} oldValue old value
 * @param {String} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"1C383D02-48EB-4808-981E-EEC67CA4A4E0"}
 */
function onDataChangeFecha(oldValue, newValue, event) {
	globals.gMes 	= newValue.substring(0,newValue.indexOf('|'))
	globals.gAnio	= newValue.substring(newValue.indexOf('|')+1,newValue.length)
	return true
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"5322A410-2225-496F-BA07-BEEADE988B4F"}
 */
function onShow(firstShow, event) {
	globals.gvarios = 3
}
